using System;
using System.Reflection;
using System.Windows.Forms;
using FBroSharp;
using FBroSharp.Const;
using FBroSharp.Event;
using FBroSharp.Lib;

namespace BaseTest
{
    /// <summary>
    /// FBro初始化事件处理类
    /// </summary>
    public class InitEvent : FBroInitEvent
    {
        /// <summary>
        /// 即将处理命令行
        /// ⚠️ CRITICAL: 参数类型必须是 IFBroSharpCommandLine (带I前缀)
        /// </summary>
        public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
        {
            // 关闭GPU加速，提高稳定性
            cmd.DisableGpuBlockList();
            cmd.DisableGpuCache();

            Console.WriteLine($"OnBeforeCommandLineProcessing: {process_type}, IsReadOnly: {cmd.IsReadOnly()}, Program: {cmd.GetProgram()}");
        }

        /// <summary>
        /// 初始化完毕
        /// </summary>
        public override void OnContextInitialized()
        {
            Console.WriteLine("OnContextInitialized: FBro框架上下文初始化完成");
        }

        /// <summary>
        /// 子进程启动前
        /// </summary>
        public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
        {
            Console.WriteLine($"OnBeforeChildProcessLaunch: IsReadOnly: {command_line.IsReadOnly()}, Program: {command_line.GetProgram()}");
        }

        /// <summary>
        /// 进程间消息接收
        /// </summary>
        public override bool OnProcessMessageReceived(IFBroSharpBrowser browser, IFBroSharpFrame frame, FBroSharpProcessId source_process, IFBroSharpProcessMessage message)
        {
            var list = message.GetArgumentList();
            Console.WriteLine($"OnProcessMessageReceived: {message.GetName()}, Arg0: {list.GetString(0)}");
            
            //返回真才会释放当前数据，代表数据处理完了
            return true;
        }
    }
}

